package com.thesis.client;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.thesis.conectify.Connectify;
import com.thesis.object.Bill;
import com.thesis.object.DishDetailAddBill;
import com.thesis.object.ListBillDescription;
import com.thesis.object.ListServeTableDetail;
import com.thesis.utils.Defines;
import com.thesis.utils.Utils;

public class BillDetailTableActivity extends Activity {
	ProgressDialog pDialog;
	TextView tv_totalcost;
	TextView tv_tablename;
	TextView tv_starttime;
	ImageView btn_confirm; 
	ImageView btn_payment;
	ListView lv_food;
	Bill bill;
	ArrayList<Integer> arr_serve;
	ArrayList<DishDetailAddBill> arr_modify;
	String[] receive;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bill_detail_table);
		 receive = getIntent().getStringArrayExtra(
					Defines.ID_TABLE_BILL_CUSTOMER);
		//table_name = getIntent().getStringExtra(Defines.ID_TABLE_BILL_CUSTOMER);
		tv_totalcost = (TextView) findViewById(R.id.bdt_tv_totalcost);
		tv_tablename = (TextView) findViewById(R.id.bdt_tv_tablename);
		tv_starttime = (TextView) findViewById(R.id.bdt_tv_starttime);
		btn_confirm = (ImageView) findViewById(R.id.bdt_b_confirm);
		btn_payment = (ImageView) findViewById(R.id.bdt_b_payment);
		
		
		
		new getBillWithTableId().execute();
		
		
		btn_confirm.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Log.d("aaaa", arr_modify.size() + "-" +arr_serve.size());
				new AlertDialog.Builder(BillDetailTableActivity.this)
				.setTitle("Xác nhận thay đổi")
				.setMessage("Bạn có chắc chắn?")
				.setPositiveButton(
						"Xác nhận",
						new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface dialog,
									int which) {
								
								if (arr_modify.size() > 0 ){
									new ModifyNumberCall().execute();
								}
								
								if (arr_serve.size() >0){
									new UpdateServeDish().execute();
								}				
								ListServeTableDetail.getInstance().clear();
								 new getBillWithTableId().execute();
							}
						})
				.setNegativeButton(
						"Hủy",
						new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface dialog,
									int which) {
								// do nothing
							}
						}).show();
				
			}
		});
		
		btn_payment.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new AlertDialog.Builder(BillDetailTableActivity.this)
				.setTitle("Thanh toán")
				.setMessage("Bạn có chắc chắn?")
				.setPositiveButton(
						"Xác nhận",
						new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface dialog,
									int which) {
								new Payment().execute();
								
								ListServeTableDetail.getInstance().clear();
								 finish();
							}
						})
				.setNegativeButton(
						"Hủy",
						new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface dialog,
									int which) {
								// do nothing
							}
						}).show();
			}
		});
		//tv_totalcost
		
		
		
	}
	@Override
	protected void onDestroy() {
		ListServeTableDetail.getInstance().clear();
		super.onDestroy();
	}

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.bill_detail_table, menu);
//		return true;
//	}
	
	class getBillWithTableId extends AsyncTask<String, String, String>{
		@Override
		protected void onPreExecute() {
			pDialog = new ProgressDialog(BillDetailTableActivity.this);
			pDialog.setMessage(getResources().getString(R.string.process));
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
		}
		@Override
		protected String doInBackground(String... params) {
			Connectify connect = new Connectify(BillDetailTableActivity.this);
			connect.getListBillAvailableByUser(Integer.parseInt(receive[0]));
			

			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			pDialog.dismiss();
			Log.d("listsetver detail ", ListServeTableDetail.getInstance().getListServeTableDetail().size()+"");
			
			bill = Utils.sharedInstance().getBill(receive[1]);
			if (bill.getListDishDes().getListBill().size() == 0){
				
			}
			lv_food = (ListView) findViewById(R.id.bdt_lv_item_bill_detail);
			arr_modify = new ArrayList<DishDetailAddBill>();
			arr_serve = new ArrayList<Integer>();
			if (bill.getListDishDes().getListBill().size() == 0){
				lv_food.setAdapter(new BillDetailTableAdapter(BillDetailTableActivity.this, new ListBillDescription(),arr_serve,arr_modify));
			}else{
				lv_food.setAdapter(new BillDetailTableAdapter(BillDetailTableActivity.this, bill.getListDishDes(),arr_serve,arr_modify));
			}
			
			int count = 0 ; 
			for (int i = 0 ; i < bill.getListDishDes().getListBill().size();i++){
				if (!bill.getListDishDes().getListBill().get(i).getServeTime().equals("null")){
					count += bill.getListDishDes().getListBill().get(i).getCost() * bill.getListDishDes().getListBill().get(i).getNumber();
				}
			}
			tv_totalcost.setText(count+" đồng");
			tv_tablename.setText("Bàn " + receive[1]);
			tv_starttime.setText(bill.getStartTime());
			super.onPostExecute(result);
		}
		
		
	}
	
	class UpdateServeDish extends AsyncTask<String, String, String>{
		
		@Override
		protected String doInBackground(String... params) {
			Connectify connect = new Connectify(BillDetailTableActivity.this);
			
			connect.serveDishes(bill.getId(), arr_serve);
			return null;
		}
		
		
		
	}
	
	class ModifyNumberCall extends AsyncTask<String, String, String>{

		@Override
		protected String doInBackground(String... params) {
			
			Connectify connect = new Connectify(BillDetailTableActivity.this);
			connect.modifyDishInBill(bill.getId(), arr_modify);
			return null;
		}
		
	}
	
	class Payment extends AsyncTask<String, String, String>{

		@Override
		protected String doInBackground(String... params) {
			Connectify connect = new Connectify(BillDetailTableActivity.this);
			connect.paymentBill(bill.getId());
			return null;
		}		
	}
}
